package ext;


/**
 *
 * 除了undo,redo命令外,计算操作入参范为(字符串间以空格隔开,输入总共三个有效字符串后回车执行结果): a + b
 *
 *
 */

import java.math.BigDecimal;
import java.util.*;

public class StackDemo2 {

    static class Operation{
        private BigDecimal b1;
        private BigDecimal b2;
        private String notation;

        public  Operation(BigDecimal b1,BigDecimal b2,String notation){
            this.b1=b1;
            this.b2=b2;
            this.notation=notation;
        }
        public String toString(){
            return b1+notation+b2;
        }

    }

    /**
     * redo操作入操作栈
     * @param operateSt
     * @param a
     */
    static Map<String,Caculate> operateMap = new HashMap<>();
    static Stack<Operation> operateSt = new Stack<>();
    static Stack<Operation> deleteSt = new Stack<>();
    /*static void pushOperateStack(Stack<Operation> operateSt, Operation a) {
        operateSt.push(a);
    }

    *//**
     * undo操作出操作栈,入删除栈
     * @param //operateSt
     * @param //deleteSt
     *//*

    static void popOperateStack(Stack<Operation> operateSt,Stack<Operation> deleteSt, Operation a) {
        operateSt.pop();
        deleteSt.push(a);
    }

    static void pushDeleteStack(Stack<Operation> st, Operation a) {
        st.push(a);
    }
    static void popOperateStack(Stack<Operation> st) {
        st.pop();
    }*/

    public static void main(String args[]) throws Exception {

       /* BigDecimal b1 = new BigDecimal("0.239");//b1.setScale(2,RoundingMode.HALF_DOWN);

        BigDecimal b2 = new BigDecimal("0.2");//b2.setScale(2,RoundingMode.HALF_DOWN);

        System.out.println(b1.divide(b2,2,RoundingMode.HALF_DOWN));*/


        ServiceLoader<Caculate> loader = ServiceLoader.load(Caculate.class);
        Iterator<Caculate> iterator = loader.iterator();
        while (iterator.hasNext()){
            Caculate caculate =  iterator.next();
            operateMap.put(caculate.getName(),caculate);
            //System.out.println(caculate.getName());

        }

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()){
           String aa =  scanner.nextLine();
            if ("redo".equals(aa)){
                //pop删除栈中的操作,并将其推入操作栈,并计算输出操作栈最前的一个操作
                if (!deleteSt.empty()){
                    Operation operation = deleteSt.pop();
                    operateSt.push(operation);
                    Caculate caculate = operateMap.get(operation.notation);
                    System.out.println(caculate.operate(operation.b1,operation.b2));

                }else {
                    System.out.println("暂无可重执行的回撤记录");
                }



            }else if ("undo".equals(aa)){
                //将上一个操作pop出操作栈,并存入删除栈,并执行当前操作栈中最新的一个操作元素的计算结果
                if (!operateSt.empty()){
                  Operation operation =  operateSt.pop();
                  deleteSt.push(operation);
                  if (operateSt.empty()){
                      System.out.println("操作数栈为空,请补充新操作");
                  }else{
                      Operation peek = operateSt.peek();
                      Caculate caculate = operateMap.get(peek.notation);
                      System.out.println(caculate.operate(peek.b1,peek.b2));
                  }

                }else{
                    //操作数据栈暂时没有操作记录元素
                    System.out.println("操作栈暂无操作记录,所以也就没有可回撤的操作");
                }

            }else{
                String[] ss = aa.split(" ");
                if (ss.length<3){
                     throw new Exception("操作入参范例如(字符串间以空格隔开,输入总共三个有效字符串后回车执行结果): a + b");
                }
                BigDecimal a1 = new BigDecimal(ss[0].trim());
                String operateName = ss[1].trim();
                BigDecimal a2 = new BigDecimal(ss[2].trim());
                Caculate caculate = operateMap.get(operateName);

                Operation operation = new Operation(a1,a2,operateName);

                operateSt.push(operation);

                System.out.println(caculate.operate(a1,a2));


            }

        }


    }
}